//
//  XJ_AlextShowView.m
//  XunJieProject
//
//  Created by xue on 2018/6/11.
//  Copyright © 2018年 Soowin. All rights reserved.
//

#import "XJ_AlextShowView.h"
///alertView  宽
#define AlertW SCREEN_WIDTH-60
@interface XJ_AlextShowView()
// 背景视图
@property (nonatomic, strong) UIView *backgroundView;
//弹窗
@property (nonatomic,retain) UIView *alertView;


@end
@implementation XJ_AlextShowView

- (instancetype)initWithImage:(NSString*)image
{
    if (self == [super init]) {
        self.frame = [UIScreen mainScreen].bounds;
        [self addSubview:self.backgroundView];
        
        self.alertView = [[UIView alloc] init];
        self.alertView.backgroundColor = [UIColor whiteColor];
        self.alertView.layer.cornerRadius = 2.0;
        self.alertView.clipsToBounds =YES;
        
        self.alertView.frame = CGRectMake(0, 0, AlertW, 240);
        self.alertView.layer.position = self.center;
        
        
        UIButton *backBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        backBtn.backgroundColor =ClearColor;
        backBtn.frame =CGRectMake(AlertW-40-2, 6, 44, 44);
        [backBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
        [self.alertView addSubview:backBtn];
        [backBtn addTarget:self action:@selector(Click_hidden) forControlEvents:UIControlEventTouchUpInside];
        
        
        UIImageView *duigou =[[UIImageView alloc]initWithFrame:CGRectMake(16, CGRectGetMaxY(backBtn.frame), AlertW-32, 230)];
        duigou.image =[UIImage imageNamed:image];
        [self.alertView addSubview:duigou];
        
        
        //计算高度
        CGFloat alertHeight = CGRectGetMaxY(duigou.frame)+10;
        self.alertView.frame = CGRectMake(0, 0, AlertW, alertHeight);
        self.alertView.layer.position = self.center;
        
        [self addSubview:self.alertView];
        
    }
    
    return self;

}
#pragma mark - 弹出 -
- (void)showXLAlertView
{
    UIWindow *rootWindow = [UIApplication sharedApplication].keyWindow;
    [rootWindow addSubview:self];
    [self creatShowAnimation];
//    [self performSelector:@selector(Click_hidden) withObject:nil afterDelay:1.8];
}
-(void)Click_hidden{
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.transform = CGAffineTransformMakeScale(0.0, 0.0);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
#pragma mark - 背景遮罩图层
- (UIView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];
        _backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
        _backgroundView.userInteractionEnabled = YES;
        UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapBackgroundView:)];
        [_backgroundView addGestureRecognizer:myTap];
    }
    return _backgroundView;
}
#pragma mark - 背景视图的点击事件
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender {
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.transform = CGAffineTransformMakeScale(0.0, 0.0);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (void)creatShowAnimation
{
    self.alertView.layer.position = self.center;
    self.alertView.transform = CGAffineTransformMakeScale(0.90, 0.90);
    [UIView animateWithDuration:0.25 delay:0 usingSpringWithDamping:0.8 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        self.alertView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    } completion:^(BOOL finished) {
    }];
}
@end
